% Obtains the eqm entry threshold v0*
% Calls Piseller, which computes for each candidate v0bar:
% - lambdastarposr given v0bar and the screening value
% - given that the marginal seller sets a positive reserve price
% - and how that translates into Surplus for a seller with V0=v0bar
% Corresponds to equation 13 in the paper.
% ---------------------------------------------- %
function[v0star,fval] = v0starfunc(cL,cP,cR,thetas,pibmat,pib0vec,pdfmat,pismat,pis0mat,v0vec,zeroreserve,screeningvalue,PAR,lambdastar)

    if PAR.generalized==0
        ds  = makedist(PAR.dist,thetas(1),thetas(2));
        CDFs = @(x) cdf(ds,x);
    else %Generalized gaussian
        CDFs = @(x) ggd_cdf(x,thetas);
    end
    % extreme values (in terms of quantile of seller value distribution)
    min_q = 0.03;
    max_q = 0.97;
    
    % Main case - calibrate v0star on the optimal lambdastar
    if nargin == 13
        if Piseller(screeningvalue,min_q,min_q,cL,cP,cR,thetas,pibmat,pib0vec,pdfmat,pismat,pis0mat,v0vec,zeroreserve,PAR) <= 0 
                warning('For this combination of data and parameters - no internal solution for v0*: at screeningvalue (r=0) no seller enters, (code as v0star=screeningvalue+0.001)')
                v0star = screeningvalue+0.001;
        elseif Piseller(screeningvalue,max_q,max_q,cL,cP,cR,thetas,pibmat,pib0vec,pdfmat,pismat,pis0mat,v0vec,zeroreserve,PAR) > 0
                warning('For this combination of data and parameters - no internal solution for v0*. Only "all enter" (code as v0star=0.999)')
                v0star = fminbnd(@(x) (CDFs(x)-0.999)^2,thetas(1)-5,thetas(1)+5);
        else
                v0starf = @(v0) Piseller(screeningvalue,v0,v0,cL,cP,cR,thetas,pibmat,pib0vec,pdfmat,pismat,pis0mat,v0vec,zeroreserve,PAR);
                [CDFv0star, fval] = fzero(@(v0) v0starf(v0),[min_q max_q],PAR.optsv0star);
                v0star = fminbnd(@(x) (CDFs(x)-CDFv0star)^2,thetas(1)-5,thetas(1)+5); 
        end

    % Comparison case - don't calibrate v0star on the optimal lambdastar, but keep lambdastar fixed (shutting down optimal bidder entry)    
    else 
        if Piseller(screeningvalue,min_q,min_q,cL,cP,cR,thetas,pibmat,pib0vec,pdfmat,pismat,pis0mat,v0vec,zeroreserve,PAR,lambdastar) <= 0 
                warning('For this combination of data and parameters - no internal solution for v0*: at screeningvalue (r=0) no seller enters, (code as v0star=screeningvalue+0.001)')
                v0star = screeningvalue+0.001;
        elseif Piseller(screeningvalue,max_q,max_q,cL,cP,cR,thetas,pibmat,pib0vec,pdfmat,pismat,pis0mat,v0vec,zeroreserve,PAR,lambdastar) > 0
                warning('For this combination of data and parameters - no internal solution for v0*. Only "all enter" (code as v0star=0.999)')
                v0star = fminbnd(@(x) (CDFs(x)-0.999)^2,thetas(1)-5,thetas(1)+5);
        else
                v0starf = @(v0) Piseller(screeningvalue,v0,v0,cL,cP,cR,thetas,pibmat,pib0vec,pdfmat,pismat,pis0mat,v0vec,zeroreserve,PAR,lambdastar);
                [CDFv0star, fval] = fzero(@(v0) v0starf(v0),[min_q max_q],PAR.optsv0star);
                v0star = fminbnd(@(x) (CDFs(x)-CDFv0star)^2,thetas(1)-5,thetas(1)+5); 
        end
        
    end
end   